﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Database;
using ConnectDatabase;
using IBE;
using Database.ConnectDatabase;
using System.Drawing;

namespace Web
{
    public partial class BookingDetails : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["BookingId"]))
                    {
                        tblBooking booking = new BookingDB().GetByKey(Convert.ToInt32(Request.QueryString["BookingId"]));
                        if (booking != null)
                        {
                            //Booking title
                            if (booking.BookingStatus.Contains("GDSOK") || booking.BookingStatus.Contains("LCCOK"))
                            {
                                lblBookingTitle.Text = "Xin chúc mừng, bạn đã đặt chỗ thành công";
                                lblBookingCode.ForeColor = Color.Green;
                            }
                            else
                            {
                                lblBookingTitle.Text = "Chúng tôi đã ghi nhận yêu cầu đặt chỗ của bạn";
                                lblBookingCode.ForeColor = Color.Black;
                            }

                            //Booking code
                            lblBookingCode.Text = booking.BookingCode;

                            //Flight information
                            InitFlightDetails(booking.Id);

                            //Passengers & price
                            List<tblPassenger> listPax = new PassengerDB().GetByBookingId(booking.Id);
                            repPax.DataSource = listPax;
                            repPax.DataBind();
                            lblGrandTotal.Text = (booking.TotalPrice.Value + booking.TotalFee.Value).ToString("#,0.##") + " " + booking.CurrencyCode;

                            //Contact
                            lblContactTitle.Text = booking.ContactTitle;
                            lblName.Text = booking.FullName;
                            lblPhone.Text = booking.Phone;
                            lblEmail.Text = booking.Email;
                            lblAddress.Text = booking.Address;
                            lblNote.Text = booking.Remark;
                        }
                        else
                        {
                            Response.Redirect("~/BookingFailed.aspx", false);
                        }
                    }
                }
                catch
                {
                    Response.Redirect("~/BookingFailed.aspx", false);
                }
            }
        }

        protected void InitFlightDetails(int bookingId)
        {
            #region Departure
            tblBookingFlightInfo departureFlt = new BookingFlightInfoDB().GetByBookingIdAndType(bookingId, false);
            if (departureFlt != null)
            {
                //Flight header
                //lblStartCityDeparture.Text = new clsBSCityDetail().getByAirportCodeOrDefault(departureFlt.DepartureAirportCode, lang).Name;
                //lblStartAirpDeparture.Text = new clsBSAirportDetail().getByCodeOrDefault(departureFlt.DepartureAirportCode, lang).Name;
                //tblCountryDetail country = new clsBSCountryDetail().getByAirportCodeOrDefault(departureFlt.DepartureAirportCode, lang);
                //if (country != null)
                //{
                //    lblStartCityDeparture.Text += ", " + country.Name;
                //}

                //lblEndCityDeparture.Text = new clsBSCityDetail().getByAirportCodeOrDefault(departureFlt.DestinationAirportCode, lang).Name;
                //lblEndAirpDeparture.Text = new clsBSAirportDetail().getByCodeOrDefault(departureFlt.DestinationAirportCode, lang).Name;
                //country = new clsBSCountryDetail().getByAirportCodeOrDefault(departureFlt.DestinationAirportCode, lang);
                //if (country != null)
                //{
                //    lblEndCityDeparture.Text += ", " + country.Name;
                //}

                //TimeSpan duration = TimeSpan.FromMinutes(departureFlt.TotalFlightTime.Value);
                //lblDurationDeparture.Text = string.Format("{0}h {1}m", duration.Days * 24 + duration.Hours, duration.Minutes);

                //Segment
                List<AvailFlt> ListAvailFlt = new List<AvailFlt>();
                List<tblBookingFlightSegment> departureSegment = new BookingFlightSegmentDB().GetByFlightId(departureFlt.Id);
                foreach (tblBookingFlightSegment segment in departureSegment)
                {
                    AvailFlt availFlt = new AvailFlt();
                    availFlt.AirlineDetail = new AirlinesDB().GetByKey(segment.AirlineCode);
                    availFlt.OpAirlineDetail = new AirlinesDB().GetByKey(segment.OpAirlineCode);
                    availFlt.StartPointAirport = new GeoAirportDB().GetByKey(segment.DepartureAirportCode);
                    availFlt.StartPointCity = new GeoCityDB().GetByKey(segment.DepartureCityCode);
                    availFlt.StartPointCountry = new GeoCountryDB().GetByKey(segment.DepartureCityCode);

                    availFlt.EndPointAirport = new GeoAirportDB().GetByKey(segment.DestinationAirportCode);
                    availFlt.EndPointCity = new GeoCityDB().GetByKey(segment.DestinationCityCode);
                    availFlt.EndPointCountry = new GeoCountryDB().GetByKey(segment.DestinationCityCode);

                    availFlt.StartDt = segment.DepartureDate.Value.ToString("yyyyMMdd");
                    availFlt.StartTm = segment.DepartureDate.Value.ToString("HHmm");

                    availFlt.EndDt = segment.ArrivalDate.Value.ToString("yyyyMMdd");
                    availFlt.EndTm = segment.ArrivalDate.Value.ToString("HHmm");

                    availFlt.StartTerminal = segment.StartTerminal.Trim();
                    availFlt.EndTerminal = segment.EndTerminal.Trim();

                    availFlt.FltNum = segment.FlightNo;
                    availFlt.FltTm = Convert.ToInt32(segment.FlightDuration.Value).ToString();

                    availFlt.ClassAdult = segment.Class.Trim();
                    availFlt.Equip = segment.AircraftIATA.Trim();

                    availFlt.FrstDwnlnStp = segment.FrstDwnlnStp != null ? segment.FrstDwnlnStp : "";
                    availFlt.FrstDwnlnStpCity = new GeoCityDB().GetByKey(segment.FrstDwnlnStp);

                    availFlt.LastDwnlnStp = segment.LastDwnlnStp != null ? segment.LastDwnlnStp : "";
                    availFlt.LastDwnlnStpCity = new GeoCityDB().GetByKey(segment.LastDwnlnStp);

                    availFlt.IsLastItem = (segment.IsLastItem != null && segment.IsLastItem.Value) ? "1" : "";
                    availFlt.ChangeStation = (segment.ChangeTerminal != null && segment.ChangeTerminal.Value) ? "1" : "";
                    availFlt.StopOvernight = (segment.OvernightStop != null && segment.OvernightStop.Value) ? "1" : "";

                    availFlt.StopTime = TimeSpan.FromMinutes(segment.StopTime.Value);

                    if (segment.ChangeAirport != null && segment.ChangeAirport.Value)
                    {
                        availFlt.ChangeAirport = availFlt.EndPointAirport.Name_En + " → " + (new GeoAirportDB().GetByKey(segment.AirportChangeTo).Name_En);
                    }

                    availFlt.IBE = departureFlt.tblBooking.IBE;

                    ListAvailFlt.Add(availFlt);
                }

                repDepartureFlightDetail.DataSource = ListAvailFlt;
                repDepartureFlightDetail.DataBind();
            }
            #endregion

            #region Return
            tblBookingFlightInfo returnFlt = new BookingFlightInfoDB().GetByBookingIdAndType(bookingId, true);
            if (returnFlt != null)
            {
                //Flight header
                pnReturn.Visible = true;
                //lblStartCityReturn.Text = new clsBSCityDetail().getByAirportCodeOrDefault(returnFlt.DepartureAirportCode, lang).Name;
                //lblStartAirpReturn.Text = new clsBSAirportDetail().getByCodeOrDefault(returnFlt.DepartureAirportCode, lang).Name;
                //tblCountryDetail country = new clsBSCountryDetail().getByAirportCodeOrDefault(returnFlt.DepartureAirportCode, lang);
                //if (country != null)
                //{
                //    lblStartCityReturn.Text += ", " + country.Name;
                //}

                //lblEndCityReturn.Text = new clsBSCityDetail().getByAirportCodeOrDefault(returnFlt.DestinationAirportCode, lang).Name;
                //lblEndAirpReturn.Text = new clsBSAirportDetail().getByCodeOrDefault(returnFlt.DestinationAirportCode, lang).Name;
                //country = new clsBSCountryDetail().getByAirportCodeOrDefault(returnFlt.DestinationAirportCode, lang);
                //if (country != null)
                //{
                //    lblEndCityReturn.Text += ", " + country.Name;
                //}

                //TimeSpan duration = TimeSpan.FromMinutes(returnFlt.TotalFlightTime.Value);
                //lblDurationReturn.Text = string.Format("{0}h {1}m", duration.Days * 24 + duration.Hours, duration.Minutes);

                //Segment
                List<AvailFlt> ListAvailFlt = new List<AvailFlt>();
                List<tblBookingFlightSegment> returnSegment = new BookingFlightSegmentDB().GetByFlightId(returnFlt.Id);
                foreach (tblBookingFlightSegment segment in returnSegment)
                {
                    AvailFlt availFlt = new AvailFlt();
                    availFlt.AirlineDetail = new AirlinesDB().GetByKey(segment.AirlineCode);
                    availFlt.OpAirlineDetail = new AirlinesDB().GetByKey(segment.OpAirlineCode);
                    availFlt.StartPointAirport = new GeoAirportDB().GetByKey(segment.DepartureAirportCode);
                    availFlt.StartPointCity = new GeoCityDB().GetByKey(segment.DepartureCityCode);
                    availFlt.StartPointCountry = new GeoCountryDB().GetByKey(segment.DepartureCityCode);

                    availFlt.EndPointAirport = new GeoAirportDB().GetByKey(segment.DestinationAirportCode);
                    availFlt.EndPointCity = new GeoCityDB().GetByKey(segment.DestinationCityCode);
                    availFlt.EndPointCountry = new GeoCountryDB().GetByKey(segment.DestinationCityCode);

                    availFlt.StartDt = segment.DepartureDate.Value.ToString("yyyyMMdd");
                    availFlt.StartTm = segment.DepartureDate.Value.ToString("HHmm");

                    availFlt.EndDt = segment.ArrivalDate.Value.ToString("yyyyMMdd");
                    availFlt.EndTm = segment.ArrivalDate.Value.ToString("HHmm");

                    availFlt.StartTerminal = segment.StartTerminal.Trim();
                    availFlt.EndTerminal = segment.EndTerminal.Trim();

                    availFlt.FltNum = segment.FlightNo;
                    availFlt.FltTm = Convert.ToInt32(segment.FlightDuration.Value).ToString();

                    availFlt.ClassAdult = segment.Class.Trim();
                    availFlt.Equip = segment.AircraftIATA.Trim();

                    availFlt.FrstDwnlnStp = segment.FrstDwnlnStp != null ? segment.FrstDwnlnStp : "";
                    availFlt.FrstDwnlnStpCity = new GeoCityDB().GetByKey(segment.FrstDwnlnStp);

                    availFlt.LastDwnlnStp = segment.LastDwnlnStp != null ? segment.LastDwnlnStp : "";
                    availFlt.LastDwnlnStpCity = new GeoCityDB().GetByKey(segment.LastDwnlnStp);

                    availFlt.IsLastItem = (segment.IsLastItem != null && segment.IsLastItem.Value) ? "1" : "";
                    availFlt.ChangeStation = (segment.ChangeTerminal != null && segment.ChangeTerminal.Value) ? "1" : "";
                    availFlt.StopOvernight = (segment.OvernightStop != null && segment.OvernightStop.Value) ? "1" : "";

                    availFlt.StopTime = TimeSpan.FromMinutes(segment.StopTime.Value);

                    if (segment.ChangeAirport != null && segment.ChangeAirport.Value)
                    {
                        availFlt.ChangeAirport = availFlt.EndPointAirport.Name_En + " → " + (new GeoAirportDB().GetByKey(segment.AirportChangeTo).Name_En);
                    }

                    availFlt.IBE = returnFlt.tblBooking.IBE;

                    ListAvailFlt.Add(availFlt);
                }

                repReturnFlightDetail.DataSource = ListAvailFlt;
                repReturnFlightDetail.DataBind();
            }
            #endregion
        }
    }
}